package com.wuyan.locationdog.ui.my


import androidx.activity.viewModels
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.bus.Bus
import com.arcsoft.commonbase.bus.bus_update_userinfo
import com.arcsoft.commonbase.ext.afterTextChange
import com.arcsoft.commonbase.ext.isTrimEmpty
import com.arcsoft.commonbase.ext.textStringTrim
import com.wuyan.locationdog.databinding.MyNicknameActivityBinding
import com.wuyan.locationdog.help.LoginHelp
import com.wuyan.locationdog.viewmodel.MyViewModel

class MyNickNameActivity : BaseBindingActivity<MyNicknameActivityBinding>() {
    private val mViewModel: MyViewModel by viewModels()

    override fun initData() {
        setTitleText("修改昵称")

        binding.apply {
//            etPhone.setText(LoginHelp.myInfoBean?.content?.nickName)
            btnEnable()
            etPhone.afterTextChange {
                btnEnable()
            }
            btnSure.setOnClickListener {
                val map = HashMap<String, Any>()
                map["nickName"] = etPhone.textStringTrim()
                mViewModel.updateUserInfo(map)
            }

        }
    }

    private fun MyNicknameActivityBinding.btnEnable() {
        if (etPhone.isTrimEmpty() || etPhone.length() < 2) {
            btnSure.alpha = 0.4F
            btnSure.isEnabled = false
        } else {
            btnSure.alpha = 1F
            btnSure.isEnabled = true
        }
    }

    override fun observe() {
        super.observe()
        mViewModel.anyBean.observe(this) {
            binding.apply {
                Bus.post(bus_update_userinfo,etPhone.textStringTrim())
                finish()
            }

        }
    }
}